﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MagicCove
{
    public partial class LaunchForm : Form
    {
        public static LaunchForm staticLF;
        public String p1DeckO;
        public String p2DeckO;
        public bool enforceRules = false;

        public LaunchForm()
        {
            InitializeComponent();
            //If save folder does not exist create it
            if (!System.IO.Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Magic Cove"))
                System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Magic Cove");
            if (!System.IO.Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Magic Cove\Card Images"))
                System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Magic Cove\Card Images");
            if (!System.IO.Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Magic Cove\Card Images\Tokens"))
                System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Magic Cove\Card Images\Tokens");
            if (!System.IO.Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Magic Cove\Decks"))
                System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Magic Cove\Decks");
        }

        private void openDEButton_Click(object sender, EventArgs e)
        {
            System.Threading.Thread dEThread = new System.Threading.Thread(new System.Threading.ThreadStart(Program.newDEThread));
            dEThread.SetApartmentState(System.Threading.ApartmentState.STA); //set appartment state to single threaded appartment for file open dialog to work. (slight performance reduction may occur)
            dEThread.Start();
        }

        private void openHelpButton_Click(object sender, EventArgs e)
        {
            System.Threading.Thread HThread = new System.Threading.Thread(new System.Threading.ThreadStart(Program.newHelpThread));
            HThread.Start();
        }

        private void openGFButton_Click(object sender, EventArgs e)
        {
            try
            {
                // create reader & open file
                TextReader deckFileTR = new StreamReader(p1DAText.Text);
                // read the text file
                p1DeckO = deckFileTR.ReadToEnd();
                // close the stream
                deckFileTR.Close();
                // create reader & open file
                TextReader deckFileTR2 = new StreamReader(p2DAText.Text);
                // read the text file
                p2DeckO = deckFileTR2.ReadToEnd();
                // close the stream
                deckFileTR2.Close();

                //need to check validity of deck strings at this point assumeing that it is valid

                staticLF = this;//refers to the latest made launch form, and as it is only opened once this is fine
                System.Threading.Thread GThread = new System.Threading.Thread(new System.Threading.ThreadStart(Program.newGameThread));
                GThread.Start();
            }
            catch
            {
                MessageBox.Show("Please enter a valid file location in the text box.");
            }
        }

        private void debugIDaPLaunchB_Click(object sender, EventArgs e)
        {
            try
            {
                // create reader & open file
                TextReader deckFileTR = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Magic Cove\Decks\p1.mcd");
                // read the text file
                p1DeckO = deckFileTR.ReadToEnd();
                // close the stream
                deckFileTR.Close();
                // create reader & open file
                TextReader deckFileTR2 = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Magic Cove\Decks\p2.mcd");
                // read the text file
                p2DeckO = deckFileTR2.ReadToEnd();
                // close the stream
                deckFileTR2.Close();

                //need to check validity of deck strings at this point assumeing that it is valid

                staticLF = this;//refers to the latest made launch form, and as it is only opened once this is fine
                System.Threading.Thread GThread = new System.Threading.Thread(new System.Threading.ThreadStart(Program.newGameThread));
                GThread.Start();
            }
            catch
            {
                MessageBox.Show("Files not found.", "ERROR");
            }
        }

        private void toggleEnforceRulesB_Click(object sender, EventArgs e)
        {
            if (enforceRules)
                toggleEnforceRulesB.Text = "Use enforced rules? Disabled.";
            else
                toggleEnforceRulesB.Text = "Use enforced rules? Enabled.";
            enforceRules = !enforceRules;
        }

        private void p1DAText_Click(object sender, EventArgs e)
        {
            p1DeckAddressOFD.ShowDialog();
            p1DAText.Text = p1DeckAddressOFD.FileName;
        }
        private void p2DAText_Click(object sender, EventArgs e)
        {
            p2DeckAddressOFD.ShowDialog();
            p2DAText.Text = p2DeckAddressOFD.FileName;
        }
    }
}
